/*
 * 上海远境文化传媒有限公司 版权所有
 */
package facade.vo.base;

import models.base.AdminUser;
import models.constants.DeletedStatus;
import models.enums.AvailableStatus;
import org.apache.commons.collections.CollectionUtils;
import play.Logger;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户值对象.
 */
public class UserVO implements Serializable {

    private static final long serialVersionUID = -8908366898420430708L;

    public String id;

    public String name;

    public String email;

    public String loginName;

    public String mobile;

    public String corpId;

    public String corpName;

    public String mechanismId;

    public String mechanismName;

    public String storeId;

    public String storeName;


    public AvailableStatus status;



    public static UserVO from(AdminUser user) {
        if (user.deleted == DeletedStatus.DELETED) {
            Logger.warn("LOG06033: 用户已经被删除，跳过：" + user);
            return null;
        }
        UserVO vo = simpleFrom(user);

        vo.status = user.status;

        if (user.corp != null) {
            vo.corpId = user.corp.id;
            vo.corpName = user.corp.name;
        }

        return vo;
    }

    public static UserVO simpleFrom(AdminUser user) {
        UserVO vo = new UserVO();

        vo.id = user.id;
        vo.name = user.name;
        vo.email = user.email;
        vo.mobile = user.mobile;
        vo.loginName = user.loginName;
        return vo;
    }

    public static List<UserVO> from(Collection<AdminUser> users) {
        if (CollectionUtils.isEmpty(users)) {
            return Collections.emptyList();
        }
        return users.stream().map(UserVO::from).filter(Objects::nonNull).collect(Collectors.toList());
    }

}
